package com.hjh.qinxin;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import Fragment.AttentionFragment;
import Fragment.BrandFragment;
import Fragment.HomeFragment;
import Fragment.MediaFragment;
import Fragment.MineFragment;

public class HomeActivity extends AppCompatActivity {
    private List<Fragment> mHomeFragment;

    private FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
        //init();
    }
    private void initView(){
        BottomNavigationView fragmentHome = findViewById(R.id.fragment_home);

        HomeFragment home = new HomeFragment();
        BrandFragment brand = new BrandFragment();
        MediaFragment media = new MediaFragment();
        AttentionFragment attention = new AttentionFragment();
        MineFragment mine = new MineFragment();

        mHomeFragment = new ArrayList<>();

        mHomeFragment.add(home);
        mHomeFragment.add(brand);
        mHomeFragment.add(media);
        mHomeFragment.add(attention);
        mHomeFragment.add(mine);

        ft = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < mHomeFragment.size(); i++){
            ft.add(R.id.home_container,mHomeFragment.get(i));
            if (i != 0){
                ft.hide(mHomeFragment.get(i));
            }
        }
        ft.commit();

        fragmentHome.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull  MenuItem item) {
                replaceHomeFragment(item.getItemId());
                return true;
                //必须为true
            }
        });

    }
    private void init(){
        TextView textView = findViewById(R.id.homeft_popularize_more);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceHomeFragment(1);
            }
        });
    }
    private void replaceHomeFragment(int id){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        for (int i =0 ; i < mHomeFragment.size(); i++){
            ft.hide(mHomeFragment.get(i));
            switch (id){
                case R.id.navi_home:
                    ft.show(mHomeFragment.get(0));
                    break;
                case R.id.navi_brand:
                    ft.show(mHomeFragment.get(1));
                    break;
                case R.id.navi_media:
                    ft.show(mHomeFragment.get(2));
                    break;
                case R.id.navi_addention:
                    ft.show(mHomeFragment.get(3));
                    break;
                case R.id.navi_mine:
                    ft.show(mHomeFragment.get(4));
                    break;
            }
        }
        ft.commit();
    }

}